package org.jeecg.modules.business.third.strategy.sap;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.business.third.entity.WmsInReceiptMaterialVo;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.dock.entity.dto.request.CustomerSuppliedMaterialHarvestRequestDTO;
import org.jeecg.modules.dock.entity.dto.request.CustomerSuppliedMaterialRequestDTO;
import org.jeecg.modules.dock.entity.dto.response.CustomerSuppliedMaterialResponseDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    客供料收货接口：WMS——>SAP【接口：A807】R53
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service(value = "r56SapWarehouse")
@Slf4j
@RequiredArgsConstructor
public class R56SapWarehouse implements SapOperateStrategy {

    private final IWmsInReceiptTaskMaterialService wmsInReceiptTaskMaterialService;
    private final SapDockingRepeaterService sapDockingRepeaterService;

    @Override
    public List operate(Map<String, Object> map) {
        log.info("普天客供料入库过账");
        List<CustomerSuppliedMaterialHarvestRequestDTO> list = new ArrayList<>();
        List<WmsInReceiptMaterialVo> wmsInReceiptMaterialVos = (List<WmsInReceiptMaterialVo>) map.get("records");
        wmsInReceiptMaterialVos.forEach(t -> {
            CustomerSuppliedMaterialHarvestRequestDTO dto = new CustomerSuppliedMaterialHarvestRequestDTO();

            SapParamUtil.validWmsInReceiptMaterial(t);

            WmsInReceiptTaskMaterial one = wmsInReceiptTaskMaterialService.getOne(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>().eq(WmsInReceiptTaskMaterial::getOriginalSystemNumber, t.getOriginalSystemNumber())
                    .eq(WmsInReceiptTaskMaterial::getOriginalSystemLineNumber, t.getOriginalSystemLineNumber()));

            SapParamUtil.validInTaskRecord(one);

            CustomerSuppliedMaterialRequestDTO customerSuppliedMaterialRequestDTO = JSONObject.parseObject(one.getExtraText(), CustomerSuppliedMaterialRequestDTO.class);
            dto.setBatchNumber(one.getBatchCode());
            dto.setProcurementVoucherCode(customerSuppliedMaterialRequestDTO.getUbDumpNumber());
            dto.setFactory(t.getStoreLocation());
            dto.setUuid(customerSuppliedMaterialRequestDTO.getUuid());
            dto.setMaterialCode(customerSuppliedMaterialRequestDTO.getMaterialCode());
            // 查询sap的仓库编码
            list.add(dto);
        });
        List<CustomerSuppliedMaterialResponseDTO> customerSuppliedMaterialResponseDTOS = sapDockingRepeaterService.customerSuppliedMaterialHarvest(list);
        return customerSuppliedMaterialResponseDTOS;
    }
}
